﻿using System;
using System.Runtime.Remoting.Messaging;
using static Program;
using static Program.CardInfo;

class Program
{
    static void Main(string[] args)
    {
        try
        {
            string cardNo = "123456789";
            string customerNm = "";
            var otherCardInfo = new OtherParamInfo
            {
                PhoneNo = "11111",
                CertifId = "2222",
                CertifTp = "C",
                Expired = "",
                CVN2 = "",
            };
            // 预校验，使用Luhn算法对卡号进行基本格式验证
            var isPassing = CheckLuhn(cardNo);
            if (!isPassing)
            {
                throw new ArgumentException("银行卡号格式不正确！");
            }
            else
            {
                var validator = new BankCardValidator(new ThirdPartyCardValidationService_B());
                validator.CheckCardValidity(cardNo, customerNm, otherCardInfo);
            }
        }
        catch (ArgumentException ex)
        {
            Console.WriteLine($"输入参数错误: {ex.Message}");
        }
        catch (Exception ex)
        {
            Console.WriteLine($"发生未知错误: {ex.Message}");
        }
    }

    ////预校验
    public static bool CheckLuhn(string data)
    {
        int sum = 0;
        int len = data.Length;
        for (int i = 0; i < len; i++)
        {
            int add = (data[i] - '0') * (2 - (i + len) % 2);
            add -= add > 9 ? 9 : 0;
            sum += add;
        }
        return sum % 10 == 0;
    }
    // 银行卡验证接口，遵循单一职责原则
    public interface ICardValidationService
    {
        bool ValidateCard(string cardNumber, string customerNm, OtherParamInfo otherCardInfo);
    }
    // 第三方银行卡验证服务实现，具体调用外部API
    public class ThirdPartyCardValidationService_A : ICardValidationService
    {
        public bool ValidateCard(string cardNumber, string customerNm, OtherParamInfo otherCardInfo)
        {
            return true;
        }
    }
    public class ThirdPartyCardValidationService_B : ICardValidationService
    {
        public bool ValidateCard(string cardNumber, string customerNm, OtherParamInfo otherCardInfo)
        {
            return true;
        }
    }
    public class BankCardValidator
    {
        private readonly ICardValidationService _validationService;
        public BankCardValidator(ICardValidationService validationService)
        {
            _validationService = validationService;
        }

        public void CheckCardValidity(string cardNumber, string customerNm, OtherParamInfo otherCardInfo)
        {
            if (_validationService.ValidateCard(cardNumber, customerNm, otherCardInfo))
            {
                Console.WriteLine("银行卡信息验证通过！");
            }
            else
            {
                Console.WriteLine("银行卡信息验证失败！");
            }
        }
    }
    public class CardInfo
    {
        /// <summary>
        /// 卡号
        /// </summary>
        public string CardNo { get; set; }
        /// <summary>
        /// 姓名
        /// </summary>
        public string CustomerNm { get; set; }
        public class OtherParamInfo
        {
            /// <summary>
            /// 手机号
            /// </summary>
            public string PhoneNo { get; set; }
            /// <summary>
            /// 	证件号
            /// </summary>
            public string CertifId { get; set; }
            /// <summary>
            /// certifTp
            /// </summary>
            public string CertifTp { get; set; }
            /// <summary>
            /// 银行卡CVN2
            /// </summary>
            public string CVN2 { get; set; }
            /// <summary>
            /// 银行卡有效期
            /// </summary>
            public string Expired { get; set; }
        }
    }

}







